{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import matplotlib.pylab as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "\n",
    "from helpers import savefig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Comparison of angle and distance-based point recovery\n",
    "\n",
    "Compare reconstruction from distances with reconstruction from angles.\n",
    "\n",
    "*The data is obtained with the script simulation_angles_distances.py*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fname = 'results/angles_distances_ICASSP.pkl'\n",
    "\n",
    "df_results = pd.read_pickle(fname)\n",
    "df_results.head()\n",
    "df_results.loc[:, 'square_size'] = df_results.loc[:, 'size']\n",
    "df_results.loc[:, 'n_it'] = df_results.n_it.astype(np.float16)\n",
    "\n",
    "# choose parameters to plot (needs to be adjusted for new data)\n",
    "plot_sizes = [1, 2, 3, 4, 5]\n",
    "plot_sigmas = [1e-3, 1e-2, 1e-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "fig.set_size_inches(6, 2.5)\n",
    "\n",
    "delta = 1\n",
    "colors = sns.color_palette('Blues', n_colors=len(plot_sizes) + delta)\n",
    "max_sigma = 10\n",
    "\n",
    "for i, size in enumerate(plot_sizes):\n",
    "    data_size = df_results[df_results.square_size == size]\n",
    "    error_angle = data_size.loc[data_size.type == 'angle']\n",
    "\n",
    "    df = error_angle[['sigma', 'error']].astype(np.float32)\n",
    "    df_means = df.groupby('sigma').agg('median')\n",
    "\n",
    "    sigmas = df_means.index.values\n",
    "    xs = sigmas[sigmas <= max_sigma]\n",
    "    ys = df_means.error.values[sigmas <= max_sigma]\n",
    "    ax.loglog(xs, ys, color=colors[i + delta], label=size)\n",
    "\n",
    "palettes = ['Greens', 'Oranges', 'Purples']\n",
    "linestyles = [':', '-.', '--']\n",
    "handles2 = []\n",
    "for j, sigma_distance in enumerate(plot_sigmas):\n",
    "    data = df_results[(df_results.type == 'angle') | ((df_results.type == 'distance') &\n",
    "                                                      (df_results.sigma == sigma_distance))]\n",
    "    colors_dist = sns.color_palette(palettes[j], n_colors=len(plot_sizes) + delta)\n",
    "    for i, size in enumerate(plot_sizes):\n",
    "        data_size = data[data.square_size == size]\n",
    "        error_distance = data_size.loc[data_size.type == 'distance', 'error'].mean()\n",
    "        ax.axhline(y=error_distance, color=colors_dist[i + delta], ls=linestyles[j])\n",
    "    h2 = ax.axhline(y=error_distance, color=colors_dist[i + delta], ls=linestyles[j])\n",
    "    handles2.append(h2)\n",
    "\n",
    "ax.set_xlabel('angle noise $\\sigma_a$')\n",
    "ax.set_ylabel('accuracy')\n",
    "ax.grid()\n",
    "handels, labels = ax.get_legend_handles_labels()\n",
    "l1 = ax.legend(handels[:1] + handels[2:3] + handels[-1:],\n",
    "               labels[:1] + labels[2:3] + labels[-1:],\n",
    "               loc='lower right',\n",
    "               title='square side',\n",
    "               framealpha=1.0)\n",
    "ax.legend(handles2, ['{:1.0e}'.format(s) for s in plot_sigmas],\n",
    "          loc='upper left',\n",
    "          title='distance noise $\\sigma_d$',\n",
    "          framealpha=1.0)\n",
    "ax.add_artist(l1)\n",
    "ax.set_xlim(1e-5, 2)\n",
    "savefig('plots/comparison.pdf', fig=fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
